<template>
  <!---搜索表单-->
    <div class="search-div">
        <el-form label-width="70px" size="small">
            <el-row>
                <el-form-item label="学院名称">
                    <el-input v-model="queryDto.collegeName" style="width: 100%"
                              placeholder="请输入学院名称"></el-input>
                </el-form-item>
            </el-row>
            <el-row style="display:flex">
                <el-button type="primary" size="small" @click="searchCollege"> 搜索</el-button>
                <el-button size="small" @click="resetData">重置</el-button>
            </el-row>
        </el-form>
    </div>

  <!--添加按钮-->
    <div class="tools-div">
        <el-button type="success" size="small" @click="addShow">添 加</el-button>
    </div>

    <el-dialog v-model="dialogVisible" title="添加或修改" width="40%" @close="handleDialogClose">
        <el-form label-width="120px" ref="ruleFormRef" :model="ruleForm" :rules="rules">
            <el-form-item label="院系名称" prop="name">
                <el-input v-model="college.name"/>
            </el-form-item>
            <el-form-item label="院系代码" prop="code">
                <el-input v-model="college.code" :disabled="disable"/>
            </el-form-item>
            <el-form-item label="联系电话" prop="contactInformation">
                <el-input v-model="college.contactInformation"/>
            </el-form-item>
            <el-form-item label="地址" prop="address">
                <el-input v-model="college.address"/>
            </el-form-item>
            <el-form-item label="成立日期" prop="establishedDate">
                <el-date-picker v-model="college.establishedDate"/>
            </el-form-item>
            <el-form-item label="院长" prop="headTeacherId">
                <el-select v-model="college.headTeacherId" filterable>
                    <el-option v-for="item in teacherList" :key="item.id" :value="item.id"
                               :label="item.name"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

  <!---数据表格-->
    <el-table :data="list" style="width: 100%" border>
        <el-table-column prop="id" label="序号" width="60px" align="center"/>
        <el-table-column prop="name" label="院系名称" width="200px"/>
        <el-table-column prop="code" label="院系代码"/>
        <el-table-column prop="address" label="地址"/>
        <el-table-column prop="contactInformation" label="公用电话"/>
        <el-table-column prop="establishedDate" label="创建日期" width="100px"/>
        <el-table-column prop="headTeacherName" label="院长"/>
        <el-table-column prop="createTime" label="创建时间" width="160px" align="center"/>
        <el-table-column prop="updateTime" label="更新时间" width="160px" align="center"/>
        <el-table-column label="操作" align="center" #default="scope" fixed="right" width="140px">
            <el-button type="primary" size="small" @click="editCollege(scope.row)"> 修改</el-button>
            <el-button type="danger" size="small" @click="deleteById(scope.row)"> 删除</el-button>
        </el-table-column>
    </el-table>
    <el-pagination v-model:current-page="pageParams.page" v-model:page-size="pageParams.limit"
                   :page-sizes="[5, 10, 20, 50, 100]" @size-change="fetchData" @current-change="fetchData"
                   layout="total, sizes, prev, pager, next" :total="total"/>
</template>

<!-- script部分修改内容 -->
<script setup>
import {onMounted, ref} from 'vue'
import {DeleteCollegeById, GetCollegeListByPage, SaveCollege, UpdateCollege} from '@/api/college'
import {ElMessage, ElMessageBox} from 'element-plus'
import {FindAllTeacher} from "@/api/teacher";

const disable = ref(false)
// 删除数据
const deleteById = row => {
    ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const {code} = await DeleteCollegeById(row.id)
        if (code === 200) {
            ElMessage.success('删除成功')
            await fetchData()
        }
    })
}

// 修改按钮点击事件处理函数
const editCollege = async row => {
    college.value = row
    dialogVisible.value = true
    disable.value = true
}

//表单数据模型
const ruleForm = ref({
    id: '',
    name: '',
    code: '',
    address: '',
    contactInformation: '',
    establishedDate: '',
    headTeacherId: '',
})

const college = ref(ruleForm) // 使用ref包裹该对象，使用reactive不方便进行重置
// 获取表单组件的引用
const ruleFormRef = ref('ruleFormRef');
const handleDialogClose = () => {
    // 清除验证状态
    ruleFormRef.value.clearValidate();
    college.value = {...ruleForm}; // 重置数据为默认值
    dialogVisible.value = false
    disable.value = false
}

// 添加角色
const submit = async () => {
    ruleFormRef.value.validate(async (valid) => {
        if (valid) {
            if (!college.value.id) {
                const {code} = await SaveCollege(college.value)
                if (code === 200) {
                    dialogVisible.value = false
                    ElMessage.success('操作成功')
                    await fetchData()
                }
            } else {
                const {code} = await UpdateCollege(college.value)
                if (code === 200) {
                    dialogVisible.value = false
                    ElMessage.success('操作成功')
                    await fetchData()
                }
            }
        }
    })
}

const queryDto = ref({collegeName: ''})

const rules = {
    name: [{required: true, message: '请输入姓名', trigger: 'blur'}],
    code: [{required: true, message: '请输入学号', trigger: 'blur'}],
    contactInformation: [{required: true, message: '请输入联系方式', trigger: 'blur'}],
    address: [{required: true, message: '请输入家庭住址', trigger: 'blur'}],
    establishedDate: [{required: true, message: '请选择创建日期', trigger: 'blur'}],
    headTeacherId: [{required: true, message: '请选择所属院系', trigger: 'blur'}],
}

// 搜索按钮点击事件处理函数
const searchCollege = () => {
    fetchData()
}

// 重置按钮点击事件处理函数
const resetData = () => {
    queryDto.value = {}
}

// 分页条总记录数
let total = ref(0)

// 定义表格数据模型
let list = ref([])

// 控制对话是否展示的变量
const dialogVisible = ref(false)

const teacherList = ref([])
//进入添加
const addShow = async () => {
    dialogVisible.value = true
    disable.value = false
}

// 页面加载完毕以后请求后端接口获取数据
onMounted(async () => {
    await fetchData()
    const {data} = await FindAllTeacher()
    teacherList.value = data
})

//分页数据
const pageParamsForm = {
    page: 1, // 页码
    limit: 10, // 每页记录数
}
const pageParams = ref(pageParamsForm) // 将pageParamsForm包装成支持响应式的对象

// 远程调用后端分页查询接口
const fetchData = async () => {
    const {data} = await GetCollegeListByPage(
        pageParams.value.page,
        pageParams.value.limit,
        queryDto.value
    )
    list.value = data.list
    total.value = data.total
}
</script>

<style scoped>
.search-div {
    margin-bottom: 10px;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}

.tools-div {
    margin: 10px 0;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}
</style>
